package com.mashibing.singleton;

/**
 * 单例模式-懒汉式
 * 妄图通过减小代码块范围加锁的方式来解决多个线程并发时存在的问题，结果并不能解决
 */
public class Mgr05 {
    private static Mgr05 INSTANCE;
    private Mgr05(){}
    public static Mgr05 getInstance(){
        if (INSTANCE == null) {
            synchronized (Mgr05.class){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                INSTANCE = new Mgr05();
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i <100 ; i++) {
            new Thread(()->{
                System.out.println(Mgr05.getInstance().hashCode());
            }).start();
        }
    }
}
